<template>
  <view class="container">
    <view class="page-title">修改密码</view>

    <view class="form-container">
      <view class="form-item">
        <text class="label">原密码</text>
        <input type="text" :value="oldPassword" disabled class="input" />
      </view>

      <view class="form-item">
        <text class="label">新密码</text>
        <input type="password" v-model="newPassword" placeholder="请输入新密码" class="input" />
      </view>

      <view class="form-item">
        <text class="label">确认密码</text>
        <input type="password" v-model="confirmPassword" placeholder="请再次输入新密码" class="input" />
      </view>

      <button class="submit-btn" @click="submitModify">确认修改</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    };
  },
  onShow() {
    const info = uni.getStorageSync('studentInfo');
    console.log('==== 缓存 studentInfo ====', info);
    this.oldPassword = info?.password || '';
    console.log('==== 原密码赋值 ====', this.oldPassword);
  },
  methods: {
    submitModify() {
      if (!this.newPassword) {
        uni.showToast({ title: '请输入新密码', icon: 'none' });
        return;
      }
      if (this.newPassword.length < 6) {
        uni.showToast({ title: '新密码至少6位', icon: 'none' });
        return;
      }
      if (this.newPassword !== this.confirmPassword) {
        uni.showToast({ title: '两次密码不一致', icon: 'none' });
        return;
      }

      uni.showLoading({ title: '修改中...' });

      const studentNumber = uni.getStorageSync('studentNumber');
      uni.request({
        url: `http://localhost:8080/api/student/reset-simple?studentNumber=${studentNumber}&newPassword=${this.newPassword}`,
        method: 'PUT',
        header: { 'Content-Type': 'application/x-www-form-urlencoded' },
        success: (res) => {
          uni.hideLoading();
          const { code, message } = res.data;
          if (code === 200) {
            uni.showToast({ title: '密码已更新' });
            // 更新本地缓存
            const info = uni.getStorageSync('studentInfo');
            if (info && typeof info === 'object') {
              info.password = this.newPassword;
              uni.setStorageSync('studentInfo', info);
            }
            // 统一返回上一页（兼容 TabBar 与普通页面）
            setTimeout(() => {
              const pages = getCurrentPages();
              if (pages.length > 1) {
                uni.navigateBack();
              } else {
                uni.switchTab({ url: '/pages/index/index' });
              }
            }, 1500);
          } else {
            uni.showToast({ title: message || '更新失败', icon: 'none' });
          }
        },
        fail: () => {
          uni.hideLoading();
          uni.showToast({ title: '网络错误', icon: 'none' });
        }
      });
    }
  }
};
</script>

<style scoped>
.container {
  background-color: #f5f5f5;
  min-height: 100vh;
}
.page-title {
  font-size: 36rpx;
  font-weight: bold;
  text-align: center;
  padding: 30rpx 0;
  background-color: #fff;
  border-bottom: 1px solid #eee;
}
.form-container {
  padding: 30rpx;
  background-color: #fff;
  margin-top: 20rpx;
}
.form-item {
  display: flex;
  align-items: center;
  padding: 25rpx 0;
  border-bottom: 1px solid #eee;
}
.label {
  font-size: 30rpx;
  color: #333;
  width: 160rpx;
}
.input {
  flex: 1;
  font-size: 30rpx;
  padding: 10rpx 0;
}
.submit-btn {
  width: 100%;
  background-color: #ff5722;
  color: #fff;
  font-size: 32rpx;
  padding: 25rpx 0;
  border-radius: 8rpx;
  margin-top: 50rpx;
}
</style>